package com.zcl.app.adapter;

import java.util.List;

import com.zcl.app.common.UIHelper;
import com.zcl.funnygame.R;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;

public class HomeAppAdapter extends BaseAdapter{
	
	private LayoutInflater mInflater;
	private List<Integer> datas;
	private Context context;
	
	public HomeAppAdapter(List<Integer> data, Context ctx)
	{
		datas = data;
		context = ctx;
		mInflater = LayoutInflater.from(context);
	}
	
	@Override
	public int getCount() {
		return datas.size();
	}

	@Override
	public Object getItem(int position) {
		return datas.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		Integer type = datas.get(position);
		ViewHolder viewHolder;
		if(convertView == null){
			convertView = mInflater.inflate(R.layout.home_app_griditem, null);
			viewHolder = new ViewHolder();
			viewHolder.appIcon = (ImageView) convertView.findViewById(R.id.home_app_icon);
			viewHolder.appBtn = (Button) convertView.findViewById(R.id.home_app_btn);
			convertView.setTag(viewHolder);
		}else{
			viewHolder = (ViewHolder) convertView.getTag();
		}
		try {
			Drawable icon = context.getResources().getDrawable(type);
			if(icon != null){
				viewHolder.appIcon.setBackgroundDrawable(icon);
			}else{
				viewHolder.appIcon.setImageResource(R.drawable.loading_2);
			}
		} catch (Exception e) {
			viewHolder.appIcon.setImageResource(R.drawable.loading_2);
		}
		viewHolder.appBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				UIHelper.ToastMessage(context, "the "+position+" is clicked");
			}
		});
		return convertView;
	}
	
	class ViewHolder{
		private ImageView appIcon;
		private Button appBtn;
	}

}
